/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package UTILITARIOS;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


/**
 *
 * @author Administrador
 */
public class TexFieldInt extends JTextField{

    private JToolTip tip;
	private Popup response;
	private String toolTipStart;
	private String toolTipEnd;

	//	Valor máximo aceptado en el textfield
	private int MAX_VALUE;
	private String MESSAGE = "En esta campo solo <br>se pueden ingresar números";
	private boolean isMandatory;

	public TexFieldInt() {
		MAX_VALUE = 10;		//	Max default value = 10
		buildComponents();
		addControl();
	}

	public TexFieldInt(int maxValue) {
		this.MAX_VALUE = maxValue;
		buildComponents();
		addControl();
	}

	@Override
	public String getText() {
		if( super.getText().length() == 0 )
			return "0";
		else
			return super.getText();
	}

	private void buildComponents() {
		tip = new JToolTip();
		tip.setFont( new Font("Tahoma",Font.PLAIN,11) );

		toolTipStart = "<html><body bgcolor=\"#FDFBE8\">"+Constantes.HTML_SPACES;
		toolTipEnd = Constantes.HTML_SPACES+"</body></html>";

		tip.setTipText(toolTipStart + MESSAGE + toolTipEnd);
	}

	private void addControl() {

		addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				if( response!= null )
					response.hide();

				addIntControl(e);
			}

		});
	}

	public int getValue()
	{
		if( getText().length() == 0 )
			return 0;
		else{
			int value = Integer.parseInt(getText());
			return value;
		}
	}

	public void setValue( int value ){
		setText(String.valueOf(value));
	}

	protected void addIntControl(KeyEvent e) {
		char c = e.getKeyChar();
		int newValue = Integer.parseInt(getText())*10 + c-'0';

		if( Character.isDigit(c) && getSelectedText()!=null && getSelectedText().length()>0 )
			this.cut();
		else if ( !( Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)
				|| (c == KeyEvent.VK_ENTER) || (c == KeyEvent.CTRL_MASK))) {
			getToolkit().beep();
			showToolTip();
			e.consume();
		}
		else if( !Character.isDigit(c) || newValue>MAX_VALUE || getText().length() > Math.log10(MAX_VALUE)+1 )
			e.consume();
	}

	protected void showToolTip() {
		response = PopupFactory.getSharedInstance().getPopup(this,
				tip, getLocationOnScreen().x + 15,
				getLocationOnScreen().y + 20);

		response.show();
		Timer hideTimer = new Timer(3000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				response.hide();
			}
		});

		hideTimer.setRepeats(false);
		hideTimer.start();
		requestFocus();
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setSize(400,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new FlowLayout());

		TexFieldInt text = new TexFieldInt(125);
		text.setPreferredSize( new Dimension(150,20) );
		frame.add( text );

		frame.setVisible(true);
	}

	/**
	 * Cambia el color de fondo del textfield, indicando si el campo es obligatorio o no.
	 * amarillo = obligatorio
	 * blanco = opcional
	 * @param isMandatory
	 */
	public void setMandatory(boolean isMandatory) {
		this.isMandatory = isMandatory;

		if( isMandatory )
			setBackground(new Color(255, 255, 230));
		else
			setBackground(Color.WHITE);
	}
}
